﻿using dz.u9.jky.schema.Common;
using dz.u9.jky.schema.SM;
using System.Collections.Generic;
using UFIDA.U9.Base.DTOs;
using UFIDA.U9.ISV.SM;
using UFIDA.U9.SPR.SalePriceAdjustment;

namespace dz.inner.jky.api.service
{
    internal class SalePriceAdjustService : BaseService
    {
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public U9CResult Add(DzSalePriceAdjustmentDTO dto)
        {
            return util.U9Invoke(__Add, dto,dto.Org);
        }

        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="doc"></param>
        public void Submit(CommonDoc doc)
        {
            SubmitSalePriceAdjustmentSRV sv = new SubmitSalePriceAdjustmentSRV();

            IDCodeNameDTO dto = new IDCodeNameDTO() { Code = doc.DocNo,ID = doc.ID};
            sv.SalePriceAdjustmentDTOs = new List<IDCodeNameDTO>(){dto};

            sv.Do();
        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="doc"></param>
        public void Audit(CommonDoc doc)
        {
            ApprovedSalePriceAdjustmentSRV sv = new ApprovedSalePriceAdjustmentSRV();

            IDCodeNameDTO dto = new IDCodeNameDTO() { Code = doc.DocNo, ID = doc.ID };
            sv.SalePriceAdjustmentDTOs = new List<IDCodeNameDTO>() { dto };

            sv.Do();

        }
        private U9CResult __Add(DzSalePriceAdjustmentDTO dto)
        {
            U9CResult result = new U9CResult();

            CreateSalePriceAdjustmentSRV sv = new CreateSalePriceAdjustmentSRV();

            sv.SalePriceAdjustmentDTO = Convert(dto);

            var rtn = sv.Do();

            result.DocNo = (rtn[0].ID > 0) ? SalePriceAdjustment.Finder.FindByID(rtn[0].ID).DocNo : string.Empty;
            result.Id = rtn[0].ID;
            result.IsSuccess = true;

            return result;
        }

        private List<SalePriceAdjustmentDTO> Convert(DzSalePriceAdjustmentDTO ori)
        {
            SalePriceAdjustmentDTO dto = new SalePriceAdjustmentDTO();

            ObjectCopyUtil util = new ObjectCopyUtil();

            util.CopyProperty(ori, ref dto);


            List<SalePriceAdjustmentDTO> rtn = new List<SalePriceAdjustmentDTO>() { dto };

            return rtn;
        }

        
    }
}
